<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>    
<%@ taglib prefix="elfunc" uri="/WEB-INF/tlds/el-functions.tld" %>
<link href="/ssol2/common/editor/css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/ssol2/common/editor/js/HuskyEZCreator.js" charset="utf-8"></script>
	<c:choose>
		<c:when test="${mode == 'u'}">
			<c:url value="item_modify.action" var="URL"/>
		</c:when>
		<c:otherwise>
			<c:url value="item_write.action" var="URL"/>
		</c:otherwise>
	</c:choose>
	<c:url var="fileLink" value="download.action">
			<c:param name="dir">C:\work\ssol2\WebContent\upload\item\</c:param>
	</c:url>
	<h3>쓰기</h3>
	<script type="text/javascript">
	var oEditors = [];
	function insertIMG(fname){
		//console.log(fname)
		  var sHTML = "<img src='/ssol2/upload/editor/" + fname + "' style='cursor:hand;' border='0'>";
		  oEditors.getById["ir1"].exec("PASTE_HTML", [sHTML]);
	} 
	function fileDownload(dir,fileName,fileRealName){
		$("form[name='writeForm']").submit(function(){
			$("[name='dir']").val(dir);
			$("[name='fileName']").val(fileName);
			$("[name='fileRealName']").val(encodeURI(fileRealName));
		})
		.attr({action:"download.action"})
		.submit();
	}
	
	function del(){
		var f = document.writeForm;
		var answer = confirm("삭제 하시겠습니까?");
		if(answer){
			f.action = "item_delete.action";
			f.submit();
		}
	}
	function strip_tags (input, allowed) {
	    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
	    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
	    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
	 });
	}
	$(function(){
		$("form[name='writeForm']").validate({
			rules:{
				cate:"required",
				itemname:"required",
				price:{
					required:true,
					digits:true
				},
				color:"required",
				item_size:"required",
				amount:{
					required:true,
					digits:true
				},
				detailinfo:"required"
			},
			messages:{
				cate:"카테고리를 선택하세요",
				itemname:"상품명을 입력하세요",
				price:"가격을 입력하세요(숫자만)",
				color:"색상을 입력하세요",
				item_size:"사이즈 입력하세요",
				amount:"제고를 입력하세요(숫자만)",
				detailinfo:"상세내용을 입력하세요"
			},
			submitHandler: function(form) {
				oEditors.getById["ir1"].exec("UPDATE_IR_FIELD", []);
				$("#detailinfo").val($("#ir1").val());
				form.submit();
			},
			debug:true
		});
		$(".addFile").click(function(){
			if($(".files").size() > 2){
				alert("3개이상 추가 불가능");
				return;
			}
			var Html = 
				"<tr class=\"files\">"+
					"<th>파일</th>"+
					"<td colspan=\"3\"><input type=\"file\" name=\"uploads\"/> &nbsp;&nbsp;&nbsp;"+
					"<input type=\"button\" value=\"삭제\" class=\"button fileDel\"   "+
					"</td>"+
				"</tr>";
			$("#content").before(Html);
			$(".fileDel").bind('click',function(){
				$(this).parents('tr').remove();
			});
		});
		
		//에디터 실행
		
		nhn.husky.EZCreator.createInIFrame({
			oAppRef: oEditors,
			elPlaceHolder: "ir1",
			sSkinURI: "/ssol2/common/editor/SEditorSkin.html",
			fCreator: "createSEditorInIFrame"
		});
		
		
	});
	
	</script>
	
	<form method="post" action="${URL}" name=writeForm enctype="multipart/form-data">
	<input type="hidden" name="itemNo" value="${itemNo > 0? itemNo : ''}"/>
	<input type="hidden" name="dir"/>
	<input type="hidden" name="fileName"/>
	<input type="hidden" name="fileRealName"/>
	
	<table border="1" cellspacing="0" width="100%" class="bbs">
	<tbody>
		<tr>
			<th align="center" width="100"><label for="cate">카테고리</label></th>
			<td>
				<select name="cate" id="cate">
					<option value=""> 없음 </option>
					<c:forEach items="${category}" var="cate">
						<c:if test="${itemInfo.cate == cate}">
							<option value="${cate}" selected>${cate}</option>
						</c:if>
						<c:if test="${itemInfo.cate != cate}">
							<option value="${cate}">${cate}</option>
						</c:if>
					</c:forEach>
				</select>
			</td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="itemname">상품명</label></th>
			<td><input type="text" id="itemname" name="itemname" value="${itemInfo.itemname}"/></td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="price">가격</label></th>
			<td><input type="text" id="price" name="price" value="${itemInfo.price > 0 ? itemInfo.price : ''}"/></td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="color">색상</label></th>
			<td><input type="text" id="color" name="color" value="${itemInfo.color}"/></td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="item_size">사이즈</label></th>
			<td><input type="text" id="item_size" name="item_size" value="${itemInfo.item_size}"/></td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="amount">제고</label></th>
			<td><input type="text" id="amount" name="amount" value="${itemInfo.amount > 0 ? itemInfo.amount : ''}"/></td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="uploads">상품대표이미지</label></th>
			<td><input type="file" id="uploads" name="uploads" value=""/> &nbsp;&nbsp;&nbsp;
				<a href="javascript:fileDownload('C:\\work\\ssol2\\WebContent\\upload\\item\\','${itemInfo.filename1}','${elfunc:urlEncode(itemInfo.filerealname1)}')">
				${itemInfo.filerealname1}
				</a>
			</td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="uploads">상품이미지</label></th>
			<td><input type="file" id="uploads" name="uploads" value=""/> &nbsp;&nbsp;&nbsp;
			<a href="${fileLink}&fileName=${itemInfo.filename2}&fileRealName=${itemInfo.filerealname2}">
			${itemInfo.filerealname2}
			</a>
			</td>	
		</tr>
		<tr>
			<th align="center" width="100"><label for="uploads">상품이미지</label></th>
			<td><input type="file" id="uploads" name="uploads" value=""/> &nbsp;&nbsp;&nbsp;
			<a href="${fileLink}&fileName=${itemInfo.filename3}&fileRealName=${itemInfo.filerealname3}">
			${itemInfo.filerealname3}
			</a>
			</td>	
		</tr>
		
		<tr id="content">
			<td colspan="4">
			<p class="contents">
				<strong><label for="detailinfo">내용</label></strong>
			</p>
			<p class="contents">
			<textarea name="ir1" id="ir1" style="width:725px; height:300px; display:none;">${itemInfo.detailinfo}</textarea>
			<textarea id="detailinfo" name="detailinfo" style="width:725px;height:200px;display:none;"></textarea>
			</p>
			</td>
		</tr>
	</tbody>
	<tfoot>
		<tr>
			<td align="right" colspan="4">
				<input type="submit" class="button" value="확인"/>
				<input type="button" class="button" value="삭제" onclick="del();">
				<input type="button" class="button" value="목록" onclick="location.href='item.action?&currentPage=${currentPage}'"/>
			</td>
		</tr>
	</tfoot>
	</table>
	</form>
	<input type="button" onclick="insertIMG('asdf')" value="dd">